<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <!-- <el-col :xs="8" :sm="6" :md="8" :lg="5"> -->
        <el-form-item>
          <el-tooltip class="item" effect="dark" content="操作时间" placement="top">
            <el-date-picker size="small" style="width:200px;" v-model="filters.operationTime" type="daterange" align="left" placeholder="选择操作日期范围">
            </el-date-picker>
          </el-tooltip>
        </el-form-item>
        <!-- </el-col> -->
        <!-- <el-col :xs="8" :sm="4" :md="4" :lg="3"> -->
        <el-form-item>
          <el-input size="small" style="width:200px;" v-model.trim="filters.userId" placeholder="用户ID"></el-input>
        </el-form-item>
        <!-- </el-col> -->
        <!-- <el-col :xs="8" :sm="6" :md="4" :lg="4"> -->
        <el-form-item>
          <el-select size="small" clearable style="width:200px;" v-model="filters.level" placeholder="风险等级">
            <!-- <el-option  label="全部风险等级"  value="-1"  ></el-option> -->
            <el-option label="一级" value="1"></el-option>
            <el-option label="二级" value="2"></el-option>
            <el-option label="三级" value="3"></el-option>
          </el-select>
        </el-form-item>
        <!-- </el-col> -->
        <el-form-item style="float:right;">
          <el-button v-show="exportBtnVisible" size="small" class="button_style" type="primary" icon="upload2" @click="exportExcels">导出</el-button>
        </el-form-item>
        <el-form-item style="float:right;">
          <el-button v-show="queryBtnVisible" size="small" class="button_style" type="primary" icon="search" v-on:click="getLogs(true)">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="Logs" stripe border :height="winHeight-188" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <!-- <el-table-column type="selection" width="55">
      </el-table-column> -->
      <!-- <el-table-column prop="serialNo" label="序号" width="120" sortable>
      </el-table-column> -->
      <el-table-column prop="userId" label="用户ID">
      </el-table-column>
      <el-table-column prop="module" label="操作模块">
      </el-table-column>
      <el-table-column prop="content" label="操作内容" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="level" label="风险等级" align="center" sortable :formatter="formatStatus">
      </el-table-column>
      <el-table-column prop="operationTime" label="操作时间" sortable>
      </el-table-column>
      <el-table-column prop="ip" label="IP">
      </el-table-column>
      <!-- <el-table-column label="操作" fixed='right' width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <!-- <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button> -->

      <el-pagination style="float:right" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page" :page-sizes="[10, 20, 40, 80]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-col>
  </section>
</template>

<script>
import util from '../../common/js/util'

import commons from '../../common/js/common'
import { mapState, mapActions } from 'vuex'
//import NProgress from 'nprogress'
import { queryLog } from '../../api/api';

export default {
  data() {
    return {
      filters: {
        userId: '',
        level: '',
        operationTime: ''
      },
      Logs: [],
      lists: [],
      exportTotal: 0,
      total: 0,
      page: 1,
      pageSize: 10,
      loading: '',
      // fullscreenLoading:false,
      listLoading: false,
      sels: [],//列表选中列
      userid: '',
      IsShow: false,

      queryBtnVisible: false,
      exportBtnVisible: false
    }
  },
  computed: mapState([
    'winWidth',
    'winHeight'
  ]),
  methods: {
    formatStatus: function (row, column) {
      return row.level == '1' ? '一级' : (row.level == '2' ? '二级' : '三级');
    },
    formatTime: function (row, column) {
      return commons.getDaytime(row.operationTime, 1, false);
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getLogs(true);
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getLogs(true);
    },
    //获取用户列表
    getLogs(bool) {
      let para = new FormData();
      para.append("pageSize", bool ? this.pageSize : this.exportTotal);
      para.append("pageNumber", bool ? this.page : 1);
      para.append("userId", this.filters.userId);
      para.append("level", this.filters.level);
      para.append("startTime", commons.changeTimes(this.filters.operationTime, 'start'));
      para.append("endTime", commons.changeTimes(this.filters.operationTime, 'end'));
      this.listLoading = bool;
      // this.fullscreenLoading = bool?false:true;
      if (!bool) {
        this.loading = this.$loading({
          lock: true,
          text: '正在导出',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
      }
      queryLog(para).then((res) => {
        console.log(res);
        this.lists = res.data.result.content;
        this.total = res.data.result.totalElements;
        this.exportTotal = res.data.result.totalElements;
        this.listLoading = false;
        if (!bool) {
          if (this.exportTotal > 60000) {
            this.$message({
              message: '导出数据量不能超过60000条',
              type: 'error'
            });
          }
          else {
            this.export2Excel();
          }
          this.loading.close();

        }
        else {
          this.Logs = res.data.result.content;
        }
      });
    },
    //table导出excel
    exportExcels: function () {
      this.getLogs(false);
    },
    export2Excel: function () {
      require.ensure([], () => {
        const { export_json_to_excel } = require('../../vendor/Export2Excel');
        const tHeader = ['用户ID', 'IP', '操作模块', '操作内容', '风险等级', '操作时间'];
        const filterVal = ['userId', 'ip', 'module', 'content', 'level', 'operationTime'];
        const list = this.lists;
        const data = this.formatJson(filterVal, list);
        export_json_to_excel(tHeader, data, '日志列表excel');
      })
    },
    formatJson: function (filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },


    selsChange: function (sels) {
      this.sels = sels;
    },

    btnShow: function () {
      let thisData = JSON.parse(sessionStorage.getItem('144'));
      let _this = this;
      $.each(thisData, function (i, j) {
        if (thisData[i] == "systemLogQuery") {
          _this.queryBtnVisible = true;
        }
        if (thisData[i] == "systemLogExport") {
          _this.exportBtnVisible = true;
        }
      });
    }
  },
  mounted() {
    this.getLogs(true);
    this.btnShow();
  }
}

</script>

<style scoped>

</style>